package com.sunyur.tailor.channel.service.inner.sendorder.service.impl;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.inner.base.SunyurResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.base.SunyurEventService;
import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import com.sunyur.tailor.channel.service.inner.sendorder.param.SunyurSendOrderHeadParam;
import com.sunyur.tailor.channel.service.inner.sendorder.model.SunyurSendOrderHeadModel;
import com.sunyur.tailor.channel.service.inner.sendorder.param.SunyurSendOrderItemParam;
import com.sunyur.tailor.channel.service.inner.sendorder.model.SunyurSendOrderItemModel;
import com.sunyur.tailor.channel.service.inner.sendorder.service.SunyurSendOrderService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * @Author pengbing
 * @Description //发货单创建消息入口
 * @Date 17:08 2021/6/1
 * @Param [sendModel]
 * @return com.alibaba.fastjson.JSONObject
 **/
public class SunyurSendOrderEntranceServiceImpl implements SunyurEventService {
    private static final Logger LOGGER_INFO = LoggerFactory.getLogger(SunyurSendOrderEntranceServiceImpl.class);
    private static final Logger LOGGER_ERROR = LoggerFactory.getLogger(SunyurSendOrderEntranceServiceImpl.class);


    //商越发货单api
    @Autowired
    private SunyurSendOrderService sunyurSendOrderService;



    public SunyurResult<Boolean> create(String param) throws Exception {
        LOGGER_INFO.info("SunyurSendOrderEntranceServiceImpl deliverPurchaseTeam param:{}", param);
        SunyurResult<Boolean> result = new SunyurResult<>();
        if (StringUtils.isBlank(param)) {
            result.setErrorMessage(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
        } else {

            // 查询发货单头信息
            SunyurSendOrderHeadParam sendOrderHeadReq = new SunyurSendOrderHeadParam();
            sendOrderHeadReq.setSendOrderCode(JSON.parseObject(param, MsgResultCode.class).getCode());
            ChannelResult<SunyurSendOrderHeadModel> sendOrderHeadInfo = sunyurSendOrderService.getSendOrderHeadInfo(sendOrderHeadReq);
            if (sendOrderHeadInfo.getContent() == null) {
                result.setErrorMessage(sendOrderHeadInfo.getCode(),sendOrderHeadInfo.getMessage());
                return result;
            }

            // 查询发货单行信息
            SunyurSendOrderItemParam sendOrderItemReq = new SunyurSendOrderItemParam();
            sendOrderItemReq.setSendOrderCode(JSON.parseObject(param, MsgResultCode.class).getCode());
            ChannelResult<List<SunyurSendOrderItemModel>> sendOrderListItem = sunyurSendOrderService.getSendOrderListItem(sendOrderItemReq);
            if (sendOrderListItem.getContent() == null) {
                result.setErrorMessage(sendOrderListItem.getCode(),sendOrderListItem.getMessage());
                return result;
            }

        }
        result.setCode("00000");
        return result;
    }
}
